<?php
    /*
    * Copyright (c) 2007, Osuvaldo Ramos
    * 
    * All rights reserved.
    * 
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are
    * met:
    *
    *    * Redistributions of source code must retain the above copyright
    *      notice, this list of conditions and the following disclaimer.
    *    * Redistributions in binary form must reproduce the above copyright
    *      notice, this list of conditions and the following disclaimer in the
    *      documentation and/or other materials provided with the distribution.
    *    * Neither the name of the CoreTech nor the names of its
    *      contributors may be used to endorse or promote products derived from
    *      this software without specific prior written permission.
    * 
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * 
    * $Id: MemberInfo.php 2 2008-01-21 02:48:28Z xdracox $
    */
    
    if ( !defined('BNETCS') || !defined('ROOT') ) exit();
    
    include_once ROOT.'/mods/RankSystem/'.$CONF['mods']['ranksystem']['name'].
        '.php';
    
    class MemberInfo
    {
        var $smarty;
        
        function MemberInfo()
        {
            $this->smarty = new Smarty();
            
            $this->smarty->template_dir = ROOT.'/mods/MemberInfo';
            $this->smarty->compile_dir = $this->smarty->template_dir;
        }
        
        function displayAllMembersInfo()
        {
            global $mdb2;
            
            $sql = 'SELECT *
                FROM bnetcs_defaultrs_rank_categories
                ORDER BY rank_category_level
                    DESC';
            $cat_result = $mdb2->query($sql);
            
            $member_info = array();
            
            while ( $cat_row = $cat_result->fetchRow() )
            {
                $category_name = $cat_row['rank_category_name'];
                $cat = array();
                $cat['name'] = $category_name;
                $cat['members'] = array();
                
                $sql = sprintf('SELECT *
                    FROM bnetcs_defaultrs_ranks
                    WHERE rank_category_id = %d
                    ORDER BY rank_level
                        DESC', $cat_row['rank_category_id']);
                $rank_result = $mdb2->query($sql);
                
                while ( $rank_row = $rank_result->fetchRow() )
                {
                    $rank_name = $rank_row['rank_name'];
                    $rank_img = $rank_row['rank_image'];
                    $rank_img_dir = RankSystem::getRankImgDir();
                    
                    $sql = sprintf('SELECT member_id
                        FROM bnetcs_defaultrs_member_ranks
                        WHERE rank_id = %d', $rank_row['rank_id']);
                    $mid_result = $mdb2->query($sql);
                    
                    if ( PEAR::isError($mid_result) )
                    {
                        $msg = 'MySQL query failed: '.$mid_result->getMessage();
                        trigger_error($msg, E_USER_WARNING);
                    }
                    
                    while ( $mid_row = $mid_result->fetchRow() )
                    {
                        $sql = sprintf('SELECT *
                            FROM bnetcs_members
                            WHERE member_id = %d', $mid_row['member_id']);
                        $member_result = $mdb2->query($sql);
                        $member_row = $member_result->fetchRow();
                        $member_name = $member_row['member_login'];
                        
                        $sql = sprintf('SELECT *
                            FROM bnetcs_member_info
                            WHERE member_id = %d', $mid_row['member_id']);
                        $info_result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($info_result) )
                        {
                            $msg = 'MySQL query failed: '
                                .$info_result->getMessage();
                            trigger_error($msg, E_USER_WARNING);
                        }
                        
                        $info_row = $info_result->fetchRow();
                        
                        $cat['members'][] = array
                        (
                            'id' => $member_row['member_id'],
                            'name' => $member_name,
                            'rank' => $rank_name,
                            'rank_img' => $rank_img_dir.'/'.$rank_img,
                            'email' => $info_row['info_email'],
                            'aim' => $info_row['info_aim'],
                            'msn' => $info_row['info_msn'],
                            'real_name' => $info_row['info_name'],
                            'games' => $info_row['info_games'],
                            'last_login' => $info_row['info_last_login'],
                            'join_date' => $member_row['member_join_date']
                        );
                    }
                }
                $member_info[] = $cat;
            }
            $this->smarty->assign('member_info', $member_info);
            $this->smarty->display('all_members.tpl');
        }
        
        function onLogin($mid)
        {
            global $mdb2;
            
            $sql = sprintf('SELECT *
                FROM bnetcs_member_info
                WHERE member_id = %d', $mid);
            $result = $mdb2->query($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL query failed: '.$result->getMessage();
                trigger_error($msg, E_USER_ERROR);
            }
            
            if ( $result->numRows() == 0 )
            {
                $sql = sprintf('INSERT
                    INTO bnetcs_member_info (member_id, info_last_login)
                    VALUES (%d, %d)', $mid, time());
            }
            else
            {
                $sql = sprintf('UPDATE bnetcs_member_info
                    SET info_last_login = %d
                    WHERE member_id = %d', time(), $mid);
            }
            
            $result = $mdb2->query($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL query failed: '.$result->getMessage();
                trigger_error($msg, E_USER_ERROR);
            }
        }
        
        function displayMemberInfo($mid)
        {
            global $mdb2;
            $rs = new RankSystem();
            
            // first get the rank name
            $rank_name = $rs->getRankName($mid);
            
            // next the members (login) name and join date
            $sql = sprintf('SELECT member_login, member_join_date
                FROM bnetcs_members
                WHERE member_id = %d', $mid);
            $result = $mdb2->query($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL query failed: '.$result->getMessage();
                trigger_error($msg, E_USER_ERROR);
            }
            
            $row = $result->fetchRow();
            $member_name = $row['member_login'];
            $join_date = $row['member_join_date'];
            
            $sql = sprintf('SELECT *
                FROM bnetcs_member_info
                WHERE member_id = %d', $mid);
            $result = $mdb2->query($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL query failed: '.$result->getMessage();
                trigger_error($msg, E_USER_ERROR);
            }
            
            $row = $result->fetchRow();
            
            $this->smarty->assign('rank', $rank_name);
            $this->smarty->assign('member', $member_name);
            $this->smarty->assign('join_date', $join_date);
            $this->smarty->assign('full_name', $row['info_name']);
            $this->smarty->assign('aim', $row['info_aim']);
            $this->smarty->assign('msn', $row['info_msn']);
            $this->smarty->assign('email', $row['info_email']);
            $this->smarty->assign('games', $row['info_games']);
            
            $this->smarty->display('one_member.tpl');
        }
        
        function displayConsole()
        {
            $this->smarty->display('console.tpl');
        }
        
        function displayPage($page)
        {
            global $auth, $mdb2;
            
            switch ( $page )
            {
                case 'mi':
                {
                    if ( isset($_POST['name']) || isset($_POST['aim'])
                        || isset($_POST['msn']) || isset($_POST['email'])
                        || isset($_POST['games']) )
                    {
                        $name = htmlentities($_POST['name']);
                        $aim = htmlentities($_POST['aim']);
                        $msn = htmlentities($_POST['msn']);
                        $email = htmlentities($_POST['email']);
                        $games = htmlentities($_POST['games']);
                        
                        $sql = sprintf('UPDATE bnetcs_member_info
                            SET info_name = "%s", info_aim = "%s",
                                info_msn = "%s", info_email = "%s",
                                info_games = "%s"
                            WHERE member_id = %d', $name, $aim, $msn, $email,
                            $games, $auth->getAuthData('member_id'));
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            $msg = 'Profile successfully modified.';
                            $_SESSION['notices'] = array($msg);
                            header('Location: console.php');
                        }
                    }
                    else
                    {
                        $sql = sprintf('SELECT *
                            FROM bnetcs_member_info
                            WHERE member_id = %d',
                            $auth->getAuthData('member_id'));
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        
                        $row = $result->fetchRow();
                        
                        $this->smarty->assign('name', $row['info_name']);
                        $this->smarty->assign('email', $row['info_email']);
                        $this->smarty->assign('aim', $row['info_aim']);
                        $this->smarty->assign('msn', $row['info_msn']);
                        $this->smarty->assign('games', $row['info_games']);
                        
                        display_header();
                        $this->smarty->display('modifyinfo.tpl');
                        display_footer();
                    }
                    
                    break;
                }
            }
        }
    }
?>
